/**
 * @author   zhangyp
 * @created  2013-10-11 下午3:07:50
 * @email    zviolet@163.com
 * TODO      在百度地图上画几何图形
 */
package com.bible.fire.pad.activity.aspect;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.map.Geometry;
import com.baidu.mapapi.map.Graphic;
import com.baidu.mapapi.map.GraphicsOverlay;
import com.baidu.mapapi.map.MKMapTouchListener;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MKOLUpdateElement;
import com.baidu.mapapi.map.MKOfflineMap;
import com.baidu.mapapi.map.MKOfflineMapListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Symbol;
import com.baidu.mapapi.map.Symbol.Color;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.bible.fire.R;
import com.bible.fire.api.baidumap.BaiduMapMKOfflineListener;
import com.bible.fire.api.baidumap.BaiduMapMKTouchListener;
import com.bible.fire.api.baidumap.BaiduMapMKViewListener;
import com.bible.fire.api.baidumap.OverlayItem4BMap;
import com.bible.fire.local.AppContext;
import com.bible.fire.pad.activity.BaseActivity;
import com.bible.fire.util.AndroidUtil;
import com.bible.fire.util.SystemConstant;

public class GraphBaiduMapActivity extends BaseActivity {
    
	private MapView mMapView = null;
	private MKOfflineMap mOffline = null;
	private MapController mMapController = null;
	
	private boolean isDrawLine = false;
	
	//保存轨迹点
	private List<GeoPoint> pointList = new ArrayList<GeoPoint>();
	
	/**
	 * 用于截获屏坐标
	 */
	MKMapTouchListener mapTouchListener = null;
	
	/**
	 *  MKMapViewListener 用于处理地图事件回调
	 */
	MKMapViewListener mMapListener = null;
	
	private Button newLineBtn = null;
	
	private Button resetBtn = null;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        this.setContentView( R.layout.baidu_graph );
        mMapView = (MapView)findViewById( R.id.bmapView );
        mMapView.getController().enableClick(true);
        mMapView.getController().setZoom(14);
        mMapView.displayZoomControls(true);
        mMapView.setDoubleClickZooming(true);
        
        mMapController = mMapView.getController();
        
      //offline地图初始化
        mOffline = new MKOfflineMap();       
        mOffline.init(mMapController, new BaiduMapMKOfflineListener( mOffline ) );

        initListener();
        int num = mOffline.scan();
        Toast.makeText( this, String.format("已经安装%d个离线地图包", num), Toast.LENGTH_LONG).show();
        
        double[] positions = AndroidUtil.getCurrentLocation();
        if( positions == null ){
        	positions = SystemConstant.GPS_DEFAULT;
        }
        if( positions != null ){
        	GeoPoint gpt = new GeoPoint((int)(positions[0]*1e6), (int)(positions[1]*1e6));
            mMapController.setCenter( gpt );
            mMapView.getOverlays().clear();
            Drawable marker = getResources().getDrawable(R.drawable.icon_markf);  //得到需要标在地图上的资源
    		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());   //为maker定义位置和边界		
        	mMapView.getOverlays().add( new OverlayItem4BMap(marker, mMapView, this, gpt, "当前位置") );
        	mMapView.refresh();
        }else{
        	Toast.makeText(this, "无法获取当前位置，请确认gps信息是否正常", Toast.LENGTH_LONG).show();
        }
	}
	
	private void drawLine(){
		mMapView.getOverlays().clear();
		//构建线
  		Geometry lineGeometry = new Geometry();
  		GeoPoint[] points = new GeoPoint[pointList.size()];
  		points = pointList.toArray(points);
  		
  		lineGeometry.setPolyLine(points);
  		//设定样式
  		Symbol lineSymbol = new Symbol();
  		Symbol.Color lineColor = lineSymbol.new Color();
  		lineColor.red = 255;
  		lineColor.green = 0;
  		lineColor.blue = 0;
  		lineColor.alpha = 255;
  		lineSymbol.setLineSymbol(lineColor, 10);
  		//生成Graphic对象
  		Graphic lineGraphic = new Graphic(lineGeometry, lineSymbol);
  		
  		GraphicsOverlay graphicsOverlay = new GraphicsOverlay(mMapView);
  		mMapView.getOverlays().add(graphicsOverlay);
  		graphicsOverlay.setData( lineGraphic );
  		mMapView.refresh();
	}
	
	private void initListener() {
		mapTouchListener = new MKMapTouchListener(){
			@Override
			public void onMapClick(GeoPoint point) {
                if( isDrawLine ){
                	pointList.add( point );
                	drawLine();
                }
			}

			@Override
			public void onMapDoubleClick(GeoPoint arg0) {		
			}

			@Override
			public void onMapLongClick(GeoPoint point) {	
				if( isDrawLine ){
                	pointList.add( point );
                	drawLine();
                }
			}			
		};
		mMapView.regMapTouchListner( mapTouchListener );
		mMapListener = new BaiduMapMKViewListener();
		mMapView.regMapViewListener(AppContext.getInstance().mBMapManager, mMapListener);
		
		newLineBtn  = (Button)findViewById(R.id.newlineBtn);
		OnClickListener onClickListener = new OnClickListener(){
			@Override
			public void onClick(View v) {
				 if( isDrawLine ){
					 isDrawLine = false;
					 newLineBtn.setText("点击画线");
				 }else{
					 isDrawLine = true;
					 newLineBtn.setText("终止画线");
				 }
			}		
		};
		newLineBtn.setOnClickListener( onClickListener );
		
		resetBtn  = (Button)findViewById(R.id.resetBtn);
		OnClickListener resetBtnListener = new OnClickListener(){
			@Override
			public void onClick(View v) {
				 mMapView.getOverlays().clear();
				 if( isDrawLine ){
					 isDrawLine = false;
					 newLineBtn.setText("点击画线");
				 }
				 pointList.clear();
				 mMapView.refresh();
			}		
		};
		resetBtn.setOnClickListener( resetBtnListener );
	}
	
	@Override
    protected void onDestroy() {
		mOffline.destroy();
		if( mMapView != null ){
			mMapView.destroy();
		}
		super.onDestroy();
	}
	
	@Override
    protected void onPause(){
        mMapView.onPause();
        super.onPause();
    }
    
    @Override
    protected void onResume() {
        mMapView.onResume();
        super.onResume();
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState){
    	super.onSaveInstanceState(outState);
    	mMapView.onSaveInstanceState(outState);    	
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState){
    	super.onRestoreInstanceState(savedInstanceState);
    	mMapView.onRestoreInstanceState(savedInstanceState);
    }
}
